﻿using AnesSystem.Model;
using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Udp;
using AnesSystem.ViewModels;
using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace AnesSystem.Controls.CommonTables.FJ.YXZYY
{
    /// <summary>
    /// ShouShuShouFei.xaml 的交互逻辑
    /// </summary>
    public partial class ShuQianFangShiCtrl : CustomCommonTableCtrl
    {
        private PatientLisDataModel _patientLisData;
        private DispatcherTimer timer;
        private int i = 0;

        public PatientLisDataModel PatientLisData
        {
            get
            {
                return _patientLisData;
            }

            set
            {
                _patientLisData = value;
                RaisePropertyChanged("PatientLisData");
            }
        }
        public ShuQianFangShiCtrl()
        {
            InitializeComponent();//创建表单控件
            InitalTimer();
            IniteRadioBation();
            datetime.Value = DateTime.Now.ToString(DateFormat.DATE_FORMAT_STR_YMD_CN);
            if (DataManager.Instance.CurrentPatientLink!=null) {
                _sqzdText.Value = DataManager.Instance.CurrentPatientLink.Application.Sqzd1;
            }            
            qm.CheckChanged += CustomCheckBoxCtrl_CheckChanged_2;
            this.Loaded += thisCtrl_Loaded;
        }


        private void thisCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            if (DataManager.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_SC_CD_WJZYY))
            {
                FunctionListHeaderCtrlViewModel.Instance.DisplayButton(true, FunctionListButton.打印, FunctionListButton.刷新);
            }
            else
            {
                FunctionListHeaderCtrlViewModel.Instance.DisplayButton(true, FunctionListButton.打印, FunctionListButton.刷新, FunctionListButton.检验数据);
            }
            DataManager.Instance.IsNotNeedSaveData = false;
        }
        private void asa1_ValueChanged(object sender, EventArgs e)
        {
            CustomRadioButtonCtrl asarbo = sender as CustomRadioButtonCtrl;
            bool isCheck = (bool)asarbo.IsChecked;
            if (!(asa7.Name).Equals("E"))
            {
                if (!isCheck)
                {
                    DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.Asa.Level = asarbo.DisplayText.ToString();
                }
            }

        }
        private void CustomCheckBoxCtrl_CheckChanged_2(object sender, RoutedEventArgs e)
        {
            CustomCheckBoxCtrl ctrl = sender as CustomCheckBoxCtrl;
            bool isCheck = ctrl.IsChecked;
            if (isCheck)
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("jbsmtz", true);
                dic.Add("nl", true);
                dic.Add("petco2", true);
                dic.Add("bbf", true);
                dic.Add("ymtmz", true);
                dic.Add("rftn", true);
                dic.Add("sftn", true);
                dic.Add("bhsaqka", true);
                dic.Add("mzssyd1", mzyd.Value + "诱导平稳，插管轻柔，严格操作常规，术中情况具体分析处理，做好应急预案，维持患者生命体征平稳。");
                FillCtrlValue(dic);
                FillAndSaveXMLTableValue(dic);
            }
        }

        private void CustomTextCtrl_GotFocus(object sender, RoutedEventArgs e)
        {
            CustomTextCtrl cusCtrl = sender as CustomTextCtrl;
            CusDatePickerWin cusDatePicker = new CusDatePickerWin();
            cusDatePicker.SelectedDateTime = DateTime.Now;
            cusDatePicker.ShowDialog();
            if (cusDatePicker.Result)
            {
                cusCtrl.Value = cusDatePicker.SelectedDateTime.ToString(DateFormat.DATE_FORMAT_STR_YMD_CN);//xxxx年xx月xx日
            }
        }


        private void InitalTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += Timer_Tick;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (i < 30)
            {
                PatientLisData = Util.ExtensionMethods.GetLisData();
                if (PatientLisData != null && PatientLisData.Result == true)
                {
                    FillLisData();
                    MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                    i = 0;
                    timer.Stop();
                    return;
                }
                i++;
            }
            else
            {
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                i = 0;
                timer.Stop();
                MessageBoxCommon.ShowDialog("获取数据完成，若未获取到检验数据，请稍等几分钟重试！！！", MessageBoxCommon.OK);
            }
        }

        public override void GetLisData()
        {
            string Zyh = DataManager.Instance.CurrentPatientLink.Patient.Zyh;
            if (Zyh == null || "".Equals(Zyh))
            {
                MessageBoxCommon.ShowDialog("请输入住院号重试！！！", MessageBoxCommon.OKCANCLE);
                return;
            }
            try
            {
                Task.Factory.StartNew(delegate
                    {
                        PatientLisData = Util.ExtensionMethods.GetLisData();
                        if (PatientLisData != null)
                        {
                            //PatientLisDatas.Result = false;
                            //PatientLisDataService.Instance.AddUpdate(PatientLisDatas);
                            Dispatcher.Invoke(delegate
                                    {
                                        FillLisData();
                                        MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                                    });
                            return;
                        }
                        UdpModel udpModel = new UdpModel();
                        udpModel.PatientId = DataManager.Instance.CurrentPatientLink.Id.ToString();
                        udpModel.Zyh = Zyh;
                        udpModel.UpdateDate = DateTime.Now.ToString();
                        udpModel.AccountID = DataManager.Instance.CurrentPatientLink.Patient.Brxm;
                        UdpMangager.Instance.SendUdpServerMsg(ServiceSign.GETLISDATA, udpModel);
                        timer.Start();//执行查询方法的线程  
                    });
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void FillLisData()
        {
            Dictionary<string, object> jianYanMap = Util.ExtensionMethods.GetJianYanMap(PatientLisData.JianYan);
            DataManager.Instance.FillCtrlValue(jianYanMap);
            DataManager.Instance.FillAndSaveXMLTableValue(jianYanMap);
        }

        private void zgnmz_CheckChanged(object sender, RoutedEventArgs e)
        {

            CustomCheckBoxCtrl ctrl = sender as CustomCheckBoxCtrl;
            bool isCheck = ctrl.IsChecked;
            bool qmtrue = qm.IsChecked;
            //if (!qmtrue)
            //{
            if (isCheck)
            {
                jbsmtz.Value = true;
                nl.Value = true;
                lpky.Value = true;
                ldky.Value = true;
                mzyd.Value = mzyd.Value + "无菌操作，动作轻柔，穿刺准确；调整适当麻醉平面，维持血流动力学平稳；术毕完成拔出硬膜外导管。";
            }
            //}
        }
        private void IniteRadioBation()
        {
            new RadioButtonHelper().ChangeRadioSatus(sqfs1, sqfsf1, sqfsff1, sqfsfff1);
            new RadioButtonHelper().ChangeRadioSatus(sqfs2, sqfsf2);
            new RadioButtonHelper().ChangeRadioSatus(sqfs3, sqfsf3);
            new RadioButtonHelper().ChangeRadioSatus(sqfs4, sqfsf4);
            new RadioButtonHelper().ChangeRadioSatus(sqfs5, sqfsf5, sqfsff5);
            new RadioButtonHelper().ChangeRadioSatus(sqfs6, sqfsf6);
            new RadioButtonHelper().ChangeRadioSatus(sqfs7, sqfsf7);
            new RadioButtonHelper().ChangeRadioSatus(sqf10, sqfs10, sqfsf10);
            new RadioButtonHelper().ChangeRadioSatus(sqfs11, sqfsf11);
            new RadioButtonHelper().ChangeRadioSatus(sqf12, sqfs12, sqfsf12);
            new RadioButtonHelper().ChangeRadioSatus(sqf13, sqfs13, sqfsf13);
            new RadioButtonHelper().ChangeRadioSatus(sqf14, sqfs14, sqfsf14);
            new RadioButtonHelper().ChangeRadioSatus(xgn1, xgn2, xgn3, xgn4);
            new RadioButtonHelper().ChangeRadioSatus(asa1, asa2, asa3, asa4, asa5, asa6);
            new RadioButtonHelper().ChangeRadioSatus(shztw, shzty);
            new RadioButtonHelper().ChangeRadioSatus(shztpcea, shztpcia);

        }
    }
}
